package com.donica.wamstest.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.donica.wamstest.entity.CommonFindVO;
import com.donica.wamstest.entity.LogVO;
import com.donica.wamstest.entity.PageResult;
import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.entity.TaskStartVO;
import com.donica.wamstest.entity.UserVO;
import com.donica.wamstest.mapper.LogMapper;
import com.donica.wamstest.utils.LogUtil;

@Service
public class LogService {
	@Autowired
	LogMapper logMapper;

	public long InsertLog(List<LogVO> volist) {
		for (LogVO logVO : volist) {
			logVO.initNullItem();
		}

		return logMapper.InsertLog(volist);
	}
	
	public long InsertLog(int result, String errMsg,List<LogVO> volist) {
		String resuString = Integer.toString(result);
		for (LogVO logVO : volist) {
			logVO.setResult(resuString);
			logVO.setMsg(errMsg);
			logVO.initNullItem();
		}

		return logMapper.InsertLog(volist);
	}

	public long deleteLog(List<LogVO> volist) {
		return logMapper.deleteLog(volist);
	}

	// 通用单条日志保存
	public <T> long SaveLog(String action, int result, String errMsg, T vo) {
		List<T> volist = new ArrayList<T>();
		volist.add(vo);
		return SaveLog(action, result, errMsg, volist);
	}

	// 通用多条日志保存
	public <T> long SaveLog(String action, int result, String errMsg, List<T> volist) {

		List<LogVO> loglist = LogUtil.getLog(action, result, volist);
		return InsertLog(result,errMsg,loglist);
	}

	// 任务日志保存
	public long SaveLog(String action, int result, String errMsg, TaskStartVO vo) {
		List<LogVO> loglist = LogUtil.getTaskLog(action,vo);
		return InsertLog(result,errMsg,loglist);
	}

	public ReturnMessage searchLog(CommonFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			long total = logMapper.searchLogCount(vo);
			List<LogVO> result = logMapper.searchLog(vo);
			PageResult pageResult = new PageResult();
			pageResult.setTotalCount(total);
			pageResult.setPageSize(vo.getPageSize());
			pageResult.setCurrPage(vo.getCurrPage());
			pageResult.setResult(result);
			rtnMsg.setResult(pageResult);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}
}
